<?php
namespace app\admin\model;

 use think\facade\Db;
use think\Model;
 use think\model\concern\SoftDelete;

 class Brand extends Model
{
    use SoftDelete;
    protected $pk = 'brand_id';
    protected $deleteTime = 'delete_time';
    public static function brand($where,$whereOr)
    {
        $list = Brand::field(true)
            ->order('sort_order','asc')
            ->where($where)
            ->where(function ($query) use ($whereOr){
                $query->whereOr($whereOr);
            })
            ->select();
        return $list;
    }
    //品牌列表推荐判断
    public static function getRecommendedAttr($v,$data)
    {
        if ($data['recommended']==1){
            return '<font style="color: red">推荐</font>';
        }else{
            return '<font style="color: green">不推荐</font>';
        }
    }
    //品牌列表显示判断
    public static function getIfShowAttr($v,$data)
    {
        if ($data['if_show']==1){
            return '<font style="color: red">显示</font>';
        }else{
            return '<font style="color: green">不显示</font>';
        }
    }
    //品牌列表图片判断
    public static function getBrandLogoAttr($v,$data)
    {
        if (empty($data['brand_logo'])){
            return "<img src='/static/data/default.png' alt=''>";
        }else{
            return "<img src='/static/{$data["brand_logo"]}' alt=''>";
        }
    }
    //品牌列表添加
    public static function addBrandAttr($add)
    {
        try{
            $res = Brand::strict(true)->save($add);
        }catch (\Exception $e){
            $res = false;
        }
        return $res;
    }
    //品牌列表删除
    public static function delBrandAttr($get)
    {
        //查询该品牌是否与商品连接
        $goods = Goods::where('brand','=',$get['id'])->column('brand');
        if (empty($goods)){
            $res = Brand::destroy($get['id']);
        }else{
            $res = false;
        }
        return $res;
    }
    //品牌列表多个删除
     public static function delsBrandAttr($get)
     {
         //查询该品牌是否与商品连接
         $goods = Goods::where('brand','in',$get['id'])->column('brand');
         $id = explode(',',$get['id']);
         if (empty($goods)){
             $res = Brand::destroy($id);
         }else{
             $res = false;
         }
         return $res;
     }



}